
clear all
set maxvar 32000
set more off


global username = c(username)
global dropbox "/Users/${username}/Dropbox"
global master_file_path "${dropbox}/Engel_GFT/replication_files"

global input "$master_file_path\data/intermediate_data"

local gtools "g"
*local version "7"
*local foldno "`version'_20"
local foldno "Panel_B"

set more off

cap mkdir "${dump}/QS_V`foldno'/"  // this is just a scratch folder for temp files
capture mkdir "$input/Quasi_separability/V`foldno'/" // this is where dtas go at end

cd "$input/Quasi_separability"

global input_cpi "$input/conventional_price_indices"

global input_engel "$input/Engel_curves\Engel_V1"

global output "$input/Quasi_separability/V`foldno'/"



**************************************************************************************
*Create dataset with change in the sum of expenditures (adapted from code in next section):
*************************************************************************************

if 1==1{
cd "$input/R43R55"
use expenditures_R43R55_i_groupsV1_D.dta , clear

drop g_groupsV1 g_groupsV2 g_groupsV3

drop if i_groupsV1=="Other fuel"

rename i_groupsV1 i_products


merge m:1 i_products using "$master_file_path\data/intermediate_data/Alternative_Gs\G_Groupings_108.dta", keepusing(G_15)

drop _merge

gen xsector=1 if sector=="Rural"
replace xsector=2 if sector=="Urban"
drop sector
rename xsector sector
drop if sector!=1


drop if wt==0  | wt==.
drop if hhid==.
drop if state43==. | district43==.
drop if sector==.

gen sum_exp=mpce*Household_size


*The line below drops all the hhd characteristics we had (kids, age, caste, religion, hhd size, flag mpce etc.)
keep hhid G_15 expenditure round sum_exp


replace expenditure=0 if expenditure==.

gegen sum_exp_G_15=total(expenditure),by(hhid G_15)

keep G_15 hhid sum_exp_G_15 sum_exp round
*just keep totals per G_15 and hhid:
duplicates drop hhid G_15 ,force 
reshape wide sum_exp_G_15 , i(hhid) j(G_15)
rename sum_exp_G_152 sum_exp_G1
rename sum_exp_G_153 sum_exp_G2
rename sum_exp_G_155 sum_exp_G3

*Fill in with zeros:
replace sum_exp_G1=0 if sum_exp_G1==.
replace sum_exp_G2=0 if sum_exp_G2==.
replace sum_exp_G3=0 if sum_exp_G3==.

gen sum_exp_G_1_2_3=sum_exp_G1+sum_exp_G2+sum_exp_G3

merge 1:1 hhid using "$input/hh_shares/hh_43_55V1_D_G108_Ben.dta", nogen

cap drop if flag_mpce==1 // these have wacky mpces
compress

*This drops markets with less than 100 hhds in either round - our main estimation sample:
egen max_count_hh=max(count_hh), by (market_id)
drop if max_count_hh<100

*Note - since we are dropping the bottom 5% and top 5% of income observations, using winsorized (logexp_capw) and non-winsorized (logexp_cap) is the same in the next 2 lines: 
egen decile_dist_bounds= xtile(logexp_cap), by(round state43 sector district43 ) nq(20)
egen decile_dist= xtile(logexp_cap) if decile_dist_bounds!=1 & decile_dist_bounds!=20, by(round state43 sector district43 ) nq(9)
drop decile_dist_bounds

*Now that households are classified by round district deciles of income, calculate mean relative shares within those groups:
drop if decile_dist==.
*no need for sector, this database only has rural obs:
drop sector

collapse (mean)  sum_exp_G1 sum_exp_G2 sum_exp_G3 sum_exp_G_1_2_3 sum_exp, by(round  state43 district43 market_year_id market_id decile_dist)

*Now reshape wide so we can calculate log changes between rounds:
preserve
keep if round==43
drop market_year_id round
rename sum_exp_G1 sum_exp_G1_R43
rename sum_exp_G2 sum_exp_G2_R43
rename sum_exp_G3 sum_exp_G3_R43
rename sum_exp_G_1_2_3 sum_exp_G_1_2_3_R43
rename sum_exp sum_exp_R43
save G43.dta, replace
restore

keep if round==55
drop market_year_id round 
rename sum_exp_G1 sum_exp_G1_R55
rename sum_exp_G2 sum_exp_G2_R55
rename sum_exp_G3 sum_exp_G3_R55
rename sum_exp_G_1_2_3 sum_exp_G_1_2_3_R55
rename sum_exp sum_exp_R55

merge 1:1 state43 district43 market_id decile_dist using G43.dta
replace decile_dist=decile_dist*10
keep if _merge==3
drop _merge
save "$output/expenditures_decile_for_QS.dta", replace

erase G43.dta
}




**************************************************************************************
*(1) Create dataset with change in expenditure share at the i level over the 2 rounds:
*************************************************************************************


cd "$master_file_path\data/intermediate_data\hh_shares"

*Set this local to 1 if you want to recreate dataset:
local create_QS=1
if `create_QS'==1 {


*This has relative shares at the household level for G1, G2, G3 groups:
*use hh_shares_43_55V1_DM_G108_15_rural.dta, clear
use "$master_file_path\data/intermediate_data\hh_shares\hh_shares_43_55V1_DM_G108_15_rural.dta", clear

*This drops markets with less than 100 hhds in either round - our main estimation sample:
egen max_count_hh=max(count_hh), by (market_id)
drop if max_count_hh<100

*Note - since we are dropping the bottom 5% and top 5% of income observations, using winsorized (logexp_capw) and non-winsorized (logexp_cap) is the same in the next 2 lines: 
egen decile_dist_bounds= xtile(logexp_cap), by(round state43 sector district43 ) nq(20)
egen decile_dist= xtile(logexp_cap) if decile_dist_bounds!=1 & decile_dist_bounds!=20, by(round state43 sector district43 ) nq(9)
drop decile_dist_bounds

*Now that households are classified by round district deciles of income, calculate mean relative shares within those groups:
drop if decile_dist==.
*no need for sector, this database only has rural obs:
drop sector

collapse (mean) rshare_* logexp_cap (first) total_wt , by(round  state43 district43 market_year_id market_id decile_dist)

*Now calculate changes over rounds:

preserve
keep if round==55
keep market_id decile_dist rshare_* logexp_cap  total_wt 
rename total_wt total_wt_55
rename logexp_cap logexp_cap55
rename rshare_* rshare55_*
save rshares55, replace
restore
keep if round==43
rename total_wt total_wt_43
rename logexp_cap logexp_cap43
rename rshare_* rshare43_*
merge 1:1 market_id decile_dist using rshares55.dta

drop if _merge!=3
drop _merge
cap erase rshares55.dta

*Now calculate delta log nominal incomes over time for each decile district:
gen dlogexp_cap=logexp_cap55-logexp_cap43

*Now reshape the file to become long district-decile-i good level:
gen market_id_decile=market_id*100+decile
reshape long rshare43_ rshare55_ , i(market_id_decile) j(good_i)
drop market_id_decile round market_year_id
order state43 district43 market_id decile_dist good_i total_wt_43 total_wt_55 rshare43_ rshare55_ logexp_cap43 logexp_cap55 dlogexp_cap logexp_cap43

*Now calculate delta log shares over time for each decile district:
gen dlog_rshare=log(rshare55_)-log(rshare43_)

save "$output/QSV`version'", replace

*Reorganize the file:
local i=1 
while `i'<35 {
use "$input/conventional_price_indices/d_ln_p_goods_i_55_43_decile.dta", clear
keep if sector=="Rural"
keep state43 district43 decile_dist dist_wt good_i sum_demo_shares_`i' sum_pluto_shares_`i' d_ln_p_pluto_55_43_`i' d_ln_p_demo_55_43_`i'
keep if good_i==`i'
rename sum_demo_shares_`i' sum_demo_shares
rename sum_pluto_shares_`i'  sum_pluto_shares
rename d_ln_p_pluto_55_43_`i' d_ln_p_pluto_55_43
rename d_ln_p_demo_55_43_`i' d_ln_p_demo_55_43
tempfile delta_prices_`i'
save `delta_prices_`i''
local i=`i'+1
}

use `delta_prices_1', clear

local i=2
while `i'<35 {
append using `delta_prices_`i''
local i=`i'+1
}


save "$output/d_ln_p_goods_i_55_43_long.dta", replace


*Now make database "wide" so we have the prices of other goods as additional variables:


local i=1
while `i'<35 {
use "$output/d_ln_p_goods_i_55_43_long.dta",clear 
keep if good_i==`i'
drop good_i dist_wt
rename sum_demo_shares sum_demo_shares_good_`i'
rename sum_pluto_shares sum_pluto_shares_good_`i'
rename d_ln_p_pluto_55_43 d_ln_p_pluto_55_43_good_`i'
rename d_ln_p_demo_55_43 d_ln_p_demo_55_43_`i'
tempfile delta_prices_`i'_w
save `delta_prices_`i'_w'
local i=`i'+1
}

use `delta_prices_1_w', clear

local i=2
while `i'<35 {
merge 1:1 state43 district43 decile_dist using `delta_prices_`i'_w', nogen

local i=`i'+1
}


merge 1:m state43 district43 decile_dist using  "$output/d_ln_p_goods_i_55_43_long.dta", nogen

save "$output/d_ln_p_goods_i_55_43_long.dta", replace



*I merge one to one since this price dataset is at the district by decile level 
merge 1:1 state43 district43 decile_dist good_i using "$output/QSV`version'.dta"


*Note that since I dropped districts with less than 100 hhds in the rshares database, those get dropped here:
keep if _merge==3
drop _merge

*make decile variable compatible with figures: 10,20, 30,...90 th percentile:
replace decile_dist=10*decile_dist

save "$output/QSV`version'.dta", replace
erase "$output/d_ln_p_goods_i_55_43_long.dta"
cap erase rshares55.dta

*Now add EV and CV at the market by decile level.
use "$input_engel\Short_g25_rshare_V1_DM_G108_15_bs0.dta",clear
keep market_id decile P4355lp_owbwg25cen_9901 W4355_lnmpcew W5543_lnmpcew P5543lp_owbwg25cen_9901 r4355total_wt r5543total_wt

gen cv=W4355_lnmpcew-P4355lp_owbwg25cen_9901
gen ev=W5543_lnmpcew-P5543lp_owbwg25cen_9901

rename decile decile_dist 
merge 1:m market_id decile_dist using "$output/QSV`version'.dta"

keep if _merge==3
drop _merge
order market_id state43 district43 decile_dist good_i 
sort market_id state43 district43 good_i  decile_dist 
save "$output/QSV`version'", replace



*Organize dataset wide so that each i good r share is in a column instead of a set of rows:

local i=1
while `i'<35 {
use "$output/QSV`version'.dta",clear
keep if good_i==`i'
keep market_id state43 district43 decile_dist rshare43_ rshare55_ dlog_rshare
gen drshare_`i'=rshare55_ -rshare43_ 
rename dlog_rshare dlog_rshare_`i'
rename rshare43_ rshare43_`i'
tempfile rshares_`i'
save `rshares_`i''
local i=`i'+1
}

use `rshares_1', clear

local i=2
while `i'<35 {
merge 1:1 market_id state43 district43 decile_dist using `rshares_`i'', nogen
local i=`i'+1
}
tempfile rshares_all
save `rshares_all'

use "$output/QSV`version'",clear
*I want a dataset here with districtXdecile cv, ev, exp_cap, relative prices. I kep one of the goods:
keep if good==1 
keep market_id state43 district43 decile_dist dist_wt total_wt_43 total_wt_55 logexp_cap43 logexp_cap55 dlogexp_cap r4355total_wt r5543total_wt W4355_lnmpcew W5543_lnmpcew cv ev P4355lp_owbwg25cen_9901 P5543lp_owbwg25cen_9901 sum_demo_shares_good_1- d_ln_p_demo_55_43_34
merge 1:1 market_id state43 district43 decile_dist using `rshares_all', nogen
save "$output/QSV`version'_wide.dta", replace



order market_id state43 district43 decile_dist dist_wt P4355lp_owbwg25cen_9901 P5543lp_owbwg25cen_9901 r4355total_wt r5543total_wt W4355_lnmpcew W5543_lnmpcew cv ev logexp_cap43, first


save "$output/QSV`version'_wide.dta",replace



**************************************************************************************
*Merge QS dataset to sum of expenditures dataset:
*************************************************************************************

use "$output/QSV`version'_wide.dta", clear
merge 1:1 market_id decile_dist using "$output/expenditures_decile_for_QS.dta", nogenerate  keepusing(sum_exp_*) 
order sum_exp_G1_R55-sum_exp_R43, after(logexp_cap43)
save "$output/QSV`version'_wide.dta", replace


}
*End of create QS test dataset


*****************************************************************************
*PREPARE DATASET  
*****************************************************************************
if 1==1{
use "$output/QSV`version'_wide.dta",clear

*Auxiliary variables:
set matsize 10000
replace r4355total_wt=int(r4355total_wt)

*average price change within G1: 

	*demo:
egen dlog_p_demo_G1=rowmean(d_ln_p_demo_55_43_25 d_ln_p_demo_55_43_26 d_ln_p_demo_55_43_3 d_ln_p_demo_55_43_2 d_ln_p_demo_55_43_15 d_ln_p_demo_55_43_5 d_ln_p_demo_55_43_22 d_ln_p_demo_55_43_17 d_ln_p_demo_55_43_9 d_ln_p_demo_55_43_27 d_ln_p_demo_55_43_1 d_ln_p_demo_55_43_6 d_ln_p_demo_55_43_20 d_ln_p_demo_55_43_28 d_ln_p_demo_55_43_11 d_ln_p_demo_55_43_21)

	*pluto:
egen dlog_p_pluto_G1=rowmean(d_ln_p_pluto_55_43_good_25 d_ln_p_pluto_55_43_good_26 d_ln_p_pluto_55_43_good_3 d_ln_p_pluto_55_43_good_2 d_ln_p_pluto_55_43_good_15 d_ln_p_pluto_55_43_good_5 d_ln_p_pluto_55_43_good_22 d_ln_p_pluto_55_43_good_17 d_ln_p_pluto_55_43_good_9 d_ln_p_pluto_55_43_good_27 d_ln_p_pluto_55_43_good_1 d_ln_p_pluto_55_43_good_6 d_ln_p_pluto_55_43_good_20 d_ln_p_pluto_55_43_good_28 d_ln_p_pluto_55_43_good_11 d_ln_p_pluto_55_43_good_21)


*average price change within G2:
	*demo
egen dlog_p_demo_G2=rowmean(d_ln_p_demo_55_43_32 d_ln_p_demo_55_43_10 d_ln_p_demo_55_43_33 d_ln_p_demo_55_43_31 d_ln_p_demo_55_43_16 d_ln_p_demo_55_43_34 d_ln_p_demo_55_43_12 d_ln_p_demo_55_43_30 d_ln_p_demo_55_43_18 d_ln_p_demo_55_43_23 d_ln_p_demo_55_43_29 d_ln_p_demo_55_43_24 d_ln_p_demo_55_43_19)

	*pluto
egen dlog_p_pluto_G2=rowmean(d_ln_p_pluto_55_43_good_32 d_ln_p_pluto_55_43_good_10 d_ln_p_pluto_55_43_good_33 d_ln_p_pluto_55_43_good_31 d_ln_p_pluto_55_43_good_16 d_ln_p_pluto_55_43_good_34 d_ln_p_pluto_55_43_good_12 d_ln_p_pluto_55_43_good_30 d_ln_p_pluto_55_43_good_18 d_ln_p_pluto_55_43_good_23 d_ln_p_pluto_55_43_good_29 d_ln_p_pluto_55_43_good_24 d_ln_p_pluto_55_43_good_19)


*average price change within G3:
	*demo:
egen dlog_p_demo_G3=rowmean(d_ln_p_demo_55_43_8 d_ln_p_demo_55_43_4 d_ln_p_demo_55_43_14 d_ln_p_demo_55_43_7 d_ln_p_demo_55_43_13)

	*pluto:
egen dlog_p_pluto_G3=rowmean(d_ln_p_pluto_55_43_good_8 d_ln_p_pluto_55_43_good_4 d_ln_p_pluto_55_43_good_14 d_ln_p_pluto_55_43_good_7 d_ln_p_pluto_55_43_good_13)

*Average price change across all goods:
	*demo:
egen dlog_p_demo_G1G2G3=rowmean(d_ln_p_demo_55_43_25 d_ln_p_demo_55_43_26 d_ln_p_demo_55_43_3 d_ln_p_demo_55_43_2 d_ln_p_demo_55_43_15 d_ln_p_demo_55_43_5 d_ln_p_demo_55_43_22 d_ln_p_demo_55_43_17 d_ln_p_demo_55_43_9 d_ln_p_demo_55_43_27 d_ln_p_demo_55_43_1 d_ln_p_demo_55_43_6 d_ln_p_demo_55_43_20 d_ln_p_demo_55_43_28 d_ln_p_demo_55_43_11 d_ln_p_demo_55_43_21      d_ln_p_demo_55_43_32 d_ln_p_demo_55_43_10 d_ln_p_demo_55_43_33 d_ln_p_demo_55_43_31 d_ln_p_demo_55_43_16 d_ln_p_demo_55_43_34 d_ln_p_demo_55_43_12 d_ln_p_demo_55_43_30 d_ln_p_demo_55_43_18 d_ln_p_demo_55_43_23 d_ln_p_demo_55_43_29 d_ln_p_demo_55_43_24 d_ln_p_demo_55_43_19                    d_ln_p_demo_55_43_8 d_ln_p_demo_55_43_4 d_ln_p_demo_55_43_14 d_ln_p_demo_55_43_7 d_ln_p_demo_55_43_13)

	*pluto:
egen dlog_p_pluto_G1G2G3=rowmean(d_ln_p_pluto_55_43_good_25 d_ln_p_pluto_55_43_good_26 d_ln_p_pluto_55_43_good_3 d_ln_p_pluto_55_43_good_2 d_ln_p_pluto_55_43_good_15 d_ln_p_pluto_55_43_good_5 d_ln_p_pluto_55_43_good_22 d_ln_p_pluto_55_43_good_17 d_ln_p_pluto_55_43_good_9 d_ln_p_pluto_55_43_good_27 d_ln_p_pluto_55_43_good_1 d_ln_p_pluto_55_43_good_6 d_ln_p_pluto_55_43_good_20 d_ln_p_pluto_55_43_good_28 d_ln_p_pluto_55_43_good_11 d_ln_p_pluto_55_43_good_21      d_ln_p_pluto_55_43_good_32 d_ln_p_pluto_55_43_good_10 d_ln_p_pluto_55_43_good_33 d_ln_p_pluto_55_43_good_31 d_ln_p_pluto_55_43_good_16 d_ln_p_pluto_55_43_good_34 d_ln_p_pluto_55_43_good_12 d_ln_p_pluto_55_43_good_30 d_ln_p_pluto_55_43_good_18 d_ln_p_pluto_55_43_good_23 d_ln_p_pluto_55_43_good_29 d_ln_p_pluto_55_43_good_24 d_ln_p_pluto_55_43_good_19                    d_ln_p_pluto_55_43_good_8 d_ln_p_pluto_55_43_good_4 d_ln_p_pluto_55_43_good_14 d_ln_p_pluto_55_43_good_7 d_ln_p_pluto_55_43_good_13)


*Calculate a price index change for all i goods in G2 and G3, respectively:
	*demo:
#delimit ;
gen wdlog_p_G1=d_ln_p_demo_55_43_25*sum_demo_shares_good_25+
d_ln_p_demo_55_43_26*sum_demo_shares_good_26+
d_ln_p_demo_55_43_3*sum_demo_shares_good_3+
d_ln_p_demo_55_43_2*sum_demo_shares_good_2+
d_ln_p_demo_55_43_15*sum_demo_shares_good_15+
d_ln_p_demo_55_43_5*sum_demo_shares_good_5+
d_ln_p_demo_55_43_22*sum_demo_shares_good_22+
d_ln_p_demo_55_43_17*sum_demo_shares_good_17+
d_ln_p_demo_55_43_9*sum_demo_shares_good_9+
d_ln_p_demo_55_43_27*sum_demo_shares_good_27+
d_ln_p_demo_55_43_1*sum_demo_shares_good_1+
d_ln_p_demo_55_43_6*sum_demo_shares_good_6+
d_ln_p_demo_55_43_20*sum_demo_shares_good_20+
d_ln_p_demo_55_43_28*sum_demo_shares_good_28+
d_ln_p_demo_55_43_11*sum_demo_shares_good_11+
d_ln_p_demo_55_43_21*sum_demo_shares_good_21;

	*pluto:;
gen wdlog_p_pluto_G1=d_ln_p_pluto_55_43_good_25*sum_pluto_shares_good_25+
d_ln_p_pluto_55_43_good_26*sum_pluto_shares_good_26+
d_ln_p_pluto_55_43_good_3*sum_pluto_shares_good_3+
d_ln_p_pluto_55_43_good_2*sum_pluto_shares_good_2+
d_ln_p_pluto_55_43_good_15*sum_pluto_shares_good_15+
d_ln_p_pluto_55_43_good_5*sum_pluto_shares_good_5+
d_ln_p_pluto_55_43_good_22*sum_pluto_shares_good_22+
d_ln_p_pluto_55_43_good_17*sum_pluto_shares_good_17+
d_ln_p_pluto_55_43_good_9*sum_pluto_shares_good_9+
d_ln_p_pluto_55_43_good_27*sum_pluto_shares_good_27+
d_ln_p_pluto_55_43_good_1*sum_pluto_shares_good_1+
d_ln_p_pluto_55_43_good_6*sum_pluto_shares_good_6+
d_ln_p_pluto_55_43_good_20*sum_pluto_shares_good_20+
d_ln_p_pluto_55_43_good_28*sum_pluto_shares_good_28+
d_ln_p_pluto_55_43_good_11*sum_pluto_shares_good_11+
d_ln_p_pluto_55_43_good_21*sum_pluto_shares_good_21;


	*demo:;
gen wdlog_p_G2=d_ln_p_demo_55_43_32*sum_demo_shares_good_32+
d_ln_p_demo_55_43_10*sum_demo_shares_good_10+
d_ln_p_demo_55_43_33*sum_demo_shares_good_33+
d_ln_p_demo_55_43_31*sum_demo_shares_good_31+
d_ln_p_demo_55_43_16*sum_demo_shares_good_16+
d_ln_p_demo_55_43_34*sum_demo_shares_good_34+
d_ln_p_demo_55_43_12*sum_demo_shares_good_12+
d_ln_p_demo_55_43_30*sum_demo_shares_good_30+
d_ln_p_demo_55_43_18*sum_demo_shares_good_18+
d_ln_p_demo_55_43_23*sum_demo_shares_good_23+
d_ln_p_demo_55_43_29*sum_demo_shares_good_29+
d_ln_p_demo_55_43_24*sum_demo_shares_good_24+
d_ln_p_demo_55_43_19*sum_demo_shares_good_19;

	*pluto;
gen wdlog_p_pluto_G2=d_ln_p_pluto_55_43_good_32*sum_demo_shares_good_32+
d_ln_p_pluto_55_43_good_10*sum_demo_shares_good_10+
d_ln_p_pluto_55_43_good_33*sum_demo_shares_good_33+
d_ln_p_pluto_55_43_good_31*sum_demo_shares_good_31+
d_ln_p_pluto_55_43_good_16*sum_demo_shares_good_16+
d_ln_p_pluto_55_43_good_34*sum_demo_shares_good_34+
d_ln_p_pluto_55_43_good_12*sum_demo_shares_good_12+
d_ln_p_pluto_55_43_good_30*sum_demo_shares_good_30+
d_ln_p_pluto_55_43_good_18*sum_demo_shares_good_18+
d_ln_p_pluto_55_43_good_23*sum_demo_shares_good_23+
d_ln_p_pluto_55_43_good_29*sum_demo_shares_good_29+
d_ln_p_pluto_55_43_good_24*sum_demo_shares_good_24+
d_ln_p_pluto_55_43_good_19*sum_demo_shares_good_19;


	*demo:;
gen wdlog_p_G3=d_ln_p_demo_55_43_8*sum_demo_shares_good_8+
d_ln_p_demo_55_43_4*sum_demo_shares_good_4+
d_ln_p_demo_55_43_14*sum_demo_shares_good_14+
d_ln_p_demo_55_43_7*sum_demo_shares_good_7+
d_ln_p_demo_55_43_13*sum_demo_shares_good_13;

	*pluto;
gen wdlog_p_pluto_G3=d_ln_p_pluto_55_43_good_8*sum_pluto_shares_good_8+
d_ln_p_pluto_55_43_good_4*sum_pluto_shares_good_4+
d_ln_p_pluto_55_43_good_14*sum_pluto_shares_good_14+
d_ln_p_pluto_55_43_good_7*sum_pluto_shares_good_7+
d_ln_p_pluto_55_43_good_13*sum_pluto_shares_good_13;


	*Demo all goods (G1, G2 and G3);
gen wdlog_p_demo_G1G2G3=
d_ln_p_demo_55_43_25*sum_demo_shares_good_25+
d_ln_p_demo_55_43_26*sum_demo_shares_good_26+
d_ln_p_demo_55_43_3*sum_demo_shares_good_3+
d_ln_p_demo_55_43_2*sum_demo_shares_good_2+
d_ln_p_demo_55_43_15*sum_demo_shares_good_15+
d_ln_p_demo_55_43_5*sum_demo_shares_good_5+
d_ln_p_demo_55_43_22*sum_demo_shares_good_22+
d_ln_p_demo_55_43_17*sum_demo_shares_good_17+
d_ln_p_demo_55_43_9*sum_demo_shares_good_9+
d_ln_p_demo_55_43_27*sum_demo_shares_good_27+
d_ln_p_demo_55_43_1*sum_demo_shares_good_1+
d_ln_p_demo_55_43_6*sum_demo_shares_good_6+
d_ln_p_demo_55_43_20*sum_demo_shares_good_20+
d_ln_p_demo_55_43_28*sum_demo_shares_good_28+
d_ln_p_demo_55_43_11*sum_demo_shares_good_11+
d_ln_p_demo_55_43_21*sum_demo_shares_good_21+
d_ln_p_demo_55_43_32*sum_demo_shares_good_32+
d_ln_p_demo_55_43_10*sum_demo_shares_good_10+
d_ln_p_demo_55_43_33*sum_demo_shares_good_33+
d_ln_p_demo_55_43_31*sum_demo_shares_good_31+
d_ln_p_demo_55_43_16*sum_demo_shares_good_16+
d_ln_p_demo_55_43_34*sum_demo_shares_good_34+
d_ln_p_demo_55_43_12*sum_demo_shares_good_12+
d_ln_p_demo_55_43_30*sum_demo_shares_good_30+
d_ln_p_demo_55_43_18*sum_demo_shares_good_18+
d_ln_p_demo_55_43_23*sum_demo_shares_good_23+
d_ln_p_demo_55_43_29*sum_demo_shares_good_29+
d_ln_p_demo_55_43_24*sum_demo_shares_good_24+
d_ln_p_demo_55_43_19*sum_demo_shares_good_19+
d_ln_p_demo_55_43_8*sum_demo_shares_good_8+
d_ln_p_demo_55_43_4*sum_demo_shares_good_4+
d_ln_p_demo_55_43_14*sum_demo_shares_good_14+
d_ln_p_demo_55_43_7*sum_demo_shares_good_7+
d_ln_p_demo_55_43_13*sum_demo_shares_good_13;


	*pluto all goods;
gen wdlog_p_pluto_G1G2G3=
d_ln_p_pluto_55_43_good_25*sum_pluto_shares_good_25+
d_ln_p_pluto_55_43_good_26*sum_pluto_shares_good_26+
d_ln_p_pluto_55_43_good_3*sum_pluto_shares_good_3+
d_ln_p_pluto_55_43_good_2*sum_pluto_shares_good_2+
d_ln_p_pluto_55_43_good_15*sum_pluto_shares_good_15+
d_ln_p_pluto_55_43_good_5*sum_pluto_shares_good_5+
d_ln_p_pluto_55_43_good_22*sum_pluto_shares_good_22+
d_ln_p_pluto_55_43_good_17*sum_pluto_shares_good_17+
d_ln_p_pluto_55_43_good_9*sum_pluto_shares_good_9+
d_ln_p_pluto_55_43_good_27*sum_pluto_shares_good_27+
d_ln_p_pluto_55_43_good_1*sum_pluto_shares_good_1+
d_ln_p_pluto_55_43_good_6*sum_pluto_shares_good_6+
d_ln_p_pluto_55_43_good_20*sum_pluto_shares_good_20+
d_ln_p_pluto_55_43_good_28*sum_pluto_shares_good_28+
d_ln_p_pluto_55_43_good_11*sum_pluto_shares_good_11+
d_ln_p_pluto_55_43_good_21*sum_pluto_shares_good_21+
d_ln_p_pluto_55_43_good_32*sum_pluto_shares_good_32+
d_ln_p_pluto_55_43_good_10*sum_pluto_shares_good_10+
d_ln_p_pluto_55_43_good_33*sum_pluto_shares_good_33+
d_ln_p_pluto_55_43_good_31*sum_pluto_shares_good_31+
d_ln_p_pluto_55_43_good_16*sum_pluto_shares_good_16+
d_ln_p_pluto_55_43_good_34*sum_pluto_shares_good_34+
d_ln_p_pluto_55_43_good_12*sum_pluto_shares_good_12+
d_ln_p_pluto_55_43_good_30*sum_pluto_shares_good_30+
d_ln_p_pluto_55_43_good_18*sum_pluto_shares_good_18+
d_ln_p_pluto_55_43_good_23*sum_pluto_shares_good_23+
d_ln_p_pluto_55_43_good_29*sum_pluto_shares_good_29+
d_ln_p_pluto_55_43_good_24*sum_pluto_shares_good_24+
d_ln_p_pluto_55_43_good_19*sum_pluto_shares_good_19+
d_ln_p_pluto_55_43_good_8*sum_pluto_shares_good_8+
d_ln_p_pluto_55_43_good_4*sum_pluto_shares_good_4+
d_ln_p_pluto_55_43_good_14*sum_pluto_shares_good_14+
d_ln_p_pluto_55_43_good_7*sum_pluto_shares_good_7+
d_ln_p_pluto_55_43_good_13*sum_pluto_shares_good_13;



*Relative share weighted price index for all G1G2G3 goods:;
*First create sum of demo shares at the decile market level for G1 G2 G3;

gen w_rshare_dlog_p_demo_G1G2G3=
(d_ln_p_demo_55_43_25*sum_demo_shares_good_25+
d_ln_p_demo_55_43_26*sum_demo_shares_good_26+
d_ln_p_demo_55_43_3*sum_demo_shares_good_3+
d_ln_p_demo_55_43_2*sum_demo_shares_good_2+
d_ln_p_demo_55_43_15*sum_demo_shares_good_15+
d_ln_p_demo_55_43_5*sum_demo_shares_good_5+
d_ln_p_demo_55_43_22*sum_demo_shares_good_22+
d_ln_p_demo_55_43_17*sum_demo_shares_good_17+
d_ln_p_demo_55_43_9*sum_demo_shares_good_9+
d_ln_p_demo_55_43_27*sum_demo_shares_good_27+
d_ln_p_demo_55_43_1*sum_demo_shares_good_1+
d_ln_p_demo_55_43_6*sum_demo_shares_good_6+
d_ln_p_demo_55_43_20*sum_demo_shares_good_20+
d_ln_p_demo_55_43_28*sum_demo_shares_good_28+
d_ln_p_demo_55_43_11*sum_demo_shares_good_11+
d_ln_p_demo_55_43_21*sum_demo_shares_good_21+
d_ln_p_demo_55_43_32*sum_demo_shares_good_32+
d_ln_p_demo_55_43_10*sum_demo_shares_good_10+
d_ln_p_demo_55_43_33*sum_demo_shares_good_33+
d_ln_p_demo_55_43_31*sum_demo_shares_good_31+
d_ln_p_demo_55_43_16*sum_demo_shares_good_16+
d_ln_p_demo_55_43_34*sum_demo_shares_good_34+
d_ln_p_demo_55_43_12*sum_demo_shares_good_12+
d_ln_p_demo_55_43_30*sum_demo_shares_good_30+
d_ln_p_demo_55_43_18*sum_demo_shares_good_18+
d_ln_p_demo_55_43_23*sum_demo_shares_good_23+
d_ln_p_demo_55_43_29*sum_demo_shares_good_29+
d_ln_p_demo_55_43_24*sum_demo_shares_good_24+
d_ln_p_demo_55_43_19*sum_demo_shares_good_19+
d_ln_p_demo_55_43_8*sum_demo_shares_good_8+
d_ln_p_demo_55_43_4*sum_demo_shares_good_4+
d_ln_p_demo_55_43_14*sum_demo_shares_good_14+
d_ln_p_demo_55_43_7*sum_demo_shares_good_7+
d_ln_p_demo_55_43_13*sum_demo_shares_good_13)/sum_exp_G_1_2_3_R43;


#delimit cr;


*G1 deviations from mean price change:
foreach i of numlist   25 26 3 2 15 5 22 17 9 27 1 6 20 28 11 21 {

gen d_ln_p_demo_55_43_G1_`i'= d_ln_p_demo_55_43_`i'

gen d_ln_p_demo_55_43_G1_sq_`i'=d_ln_p_demo_55_43_G1_`i'^2
gen d_ln_p_demo_55_43_G1_cu_`i'=d_ln_p_demo_55_43_G1_`i'^3
}

*G2 deviations from mean price change:
foreach i of numlist   32 10 33 31 16 34 12 30 18 23 29 24 19  {
gen d_ln_p_demo_55_43_G2_`i'= d_ln_p_demo_55_43_`i'

gen d_ln_p_demo_55_43_G2_sq_`i'=d_ln_p_demo_55_43_G2_`i'^2
gen d_ln_p_demo_55_43_G2_cu_`i'=d_ln_p_demo_55_43_G2_`i'^3
}

*G3 deviations from mean price change
foreach i of numlist   8 4 14 7 13  {
gen d_ln_p_demo_55_43_G3_`i'= d_ln_p_demo_55_43_`i'


gen d_ln_p_demo_55_43_G3_sq_`i'=d_ln_p_demo_55_43_G3_`i'^2
gen d_ln_p_demo_55_43_G3_cu_`i'=d_ln_p_demo_55_43_G3_`i'^3
}


*Create polynomials of welfare terms:
gen ev2=ev^2
gen ev3=ev^3
gen cv2=cv^2
gen cv3=cv^3
gen dlogexp_cap2=dlogexp_cap^2
gen dlogexp_cap3=dlogexp_cap^3


*Generate dependent variables:
gen d_ln_NG1=log(sum_exp_R55-sum_exp_G1_R55)-log(sum_exp_R43-sum_exp_G1_R43)
gen d_ln_NG2=log(sum_exp_R55-sum_exp_G2_R55)-log(sum_exp_R43-sum_exp_G2_R43)
gen d_ln_NG3=log(sum_exp_R55-sum_exp_G3_R55)-log(sum_exp_R43-sum_exp_G3_R43)
gen d_ln_NG_1_2_3=log(sum_exp_R55-sum_exp_G_1_2_3_R55)-log(sum_exp_R43-sum_exp_G_1_2_3_R43)


save "$output/QSV`version'_wide_4permutation.dta", replace
}


*First create fake database with normally distributed prices: 
if 1==1 {
use "$output/QSV`version'_wide_4permutation.dta",clear

foreach i of numlist   25 26 3 2 15 5 22 17 9 27 1 6 20 28 11 21 {
sum d_ln_p_demo_55_43_G1_`i'
global m_`i'=r(mean)
global sd_`i'=r(sd)
}


foreach i of numlist   32 10 33 31 16 34 12 30 18 23 29 24 19  {
sum d_ln_p_demo_55_43_G2_`i'
global m_`i'=r(mean)
global sd_`i'=r(sd)
}

foreach i of numlist   8 4 14 7 13  {
sum d_ln_p_demo_55_43_G3_`i'
global m_`i'=r(mean)
global sd_`i'=r(sd)
}

global obs=r(N)

	*Now create a new dataset of prices with same mean and sd as in the empirical distribution for permutation tests:

	keep market_id state43 district43 decile_dist 
	*This is just a starter dataset:
	save ${dump}/QS_V`foldno'/starter.dta, replace
	

	forvalues i=1(1)500 {
	set seed `i'
	use ${dump}/QS_V`foldno'/starter.dta, clear
	forvalues j=1(1)34 {
	display `$m_`j''
	gen d_ln_p_demo_55_43_`j'_fake=rnormal(${m_`j'}, ${sd_`j'}) 
	}
	save ${dump}/QS_V`foldno'/fake_prices_`i'.dta,replace
	}
}	











*Now run loop with fake rhs prices:



if 1==1{
tempname permutation_results_V`foldno'
postfile `permutation_results_V`foldno''  str20 type run F df double p N using "$output\permutation_resultsV`foldno'.dta",replace
local runs=500
	
	
	
forvalues r=1(1)`runs' {
use "$output/QSV`version'_wide_4permutation.dta",clear
*Define locals:
if 1==1{
*LOCAL -Change in log prices:
local G1_goods_d_ln_p ="d_ln_p_demo_55_43_25 d_ln_p_demo_55_43_26 d_ln_p_demo_55_43_3  d_ln_p_demo_55_43_2 d_ln_p_demo_55_43_15 d_ln_p_demo_55_43_5 d_ln_p_demo_55_43_22 d_ln_p_demo_55_43_17 d_ln_p_demo_55_43_9 d_ln_p_demo_55_43_27 d_ln_p_demo_55_43_1 d_ln_p_demo_55_43_6 d_ln_p_demo_55_43_20 d_ln_p_demo_55_43_28 d_ln_p_demo_55_43_11 d_ln_p_demo_55_43_21"

local G2_goods_d_ln_p ="d_ln_p_demo_55_43_32 d_ln_p_demo_55_43_10  d_ln_p_demo_55_43_33 d_ln_p_demo_55_43_31 d_ln_p_demo_55_43_16 d_ln_p_demo_55_43_34 d_ln_p_demo_55_43_12 d_ln_p_demo_55_43_30 d_ln_p_demo_55_43_18 d_ln_p_demo_55_43_23 d_ln_p_demo_55_43_29 d_ln_p_demo_55_43_24 d_ln_p_demo_55_43_19"

local G3_goods_d_ln_p ="d_ln_p_demo_55_43_8 d_ln_p_demo_55_43_4 d_ln_p_demo_55_43_14  d_ln_p_demo_55_43_7 d_ln_p_demo_55_43_13"


local G3_goods_d_ln_p_pluto ="d_ln_p_pluto_55_43_good_8 d_ln_p_pluto_55_43_good_4 d_ln_p_pluto_55_43_good_14  d_ln_p_pluto_55_43_good_7 d_ln_p_pluto_55_43_good_13"
local G2_goods_d_ln_p_pluto ="d_ln_p_pluto_55_43_good_32 d_ln_p_pluto_55_43_good_10  d_ln_p_pluto_55_43_good_33 d_ln_p_pluto_55_43_good_31 d_ln_p_pluto_55_43_good_16 d_ln_p_pluto_55_43_good_34 d_ln_p_pluto_55_43_good_12 d_ln_p_pluto_55_43_good_30 d_ln_p_pluto_55_43_good_18 d_ln_p_pluto_55_43_good_23 d_ln_p_pluto_55_43_good_29 d_ln_p_pluto_55_43_good_24 d_ln_p_pluto_55_43_good_19"


local welfare_change ="ev cv dlogexp_cap"
local welfare_change_poly="ev cv dlogexp_cap ev2 cv2 dlogexp_cap2 ev3 cv3 dlogexp_cap3" 


*Change in relative log prices locals:
	*demo:
local d_ln_p_demo_55_43_G1_="d_ln_p_demo_55_43_G1_25 d_ln_p_demo_55_43_G1_26 d_ln_p_demo_55_43_G1_3 d_ln_p_demo_55_43_G1_2 d_ln_p_demo_55_43_G1_15 d_ln_p_demo_55_43_G1_5 d_ln_p_demo_55_43_G1_22 d_ln_p_demo_55_43_G1_17 d_ln_p_demo_55_43_G1_9 d_ln_p_demo_55_43_G1_27 d_ln_p_demo_55_43_G1_1 d_ln_p_demo_55_43_G1_6 d_ln_p_demo_55_43_G1_20 d_ln_p_demo_55_43_G1_28 d_ln_p_demo_55_43_G1_11 d_ln_p_demo_55_43_G1_21"

	*pluto:
local d_ln_p_pluto_55_43_G1_="d_ln_p_pluto_55_43_G1_25 d_ln_p_pluto_55_43_G1_26 d_ln_p_pluto_55_43_G1_3 d_ln_p_pluto_55_43_G1_2 d_ln_p_pluto_55_43_G1_15 d_ln_p_pluto_55_43_G1_5 d_ln_p_pluto_55_43_G1_22 d_ln_p_pluto_55_43_G1_17 d_ln_p_pluto_55_43_G1_9 d_ln_p_pluto_55_43_G1_27 d_ln_p_pluto_55_43_G1_1 d_ln_p_pluto_55_43_G1_6 d_ln_p_pluto_55_43_G1_20 d_ln_p_pluto_55_43_G1_28 d_ln_p_pluto_55_43_G1_11 d_ln_p_pluto_55_43_G1_21"



*Change in relative log prices locals including quadratic terms :
local d_ln_p_demo_55_43_G1_squared="d_ln_p_demo_55_43_G1_25 d_ln_p_demo_55_43_G1_26 d_ln_p_demo_55_43_G1_3 d_ln_p_demo_55_43_G1_2 d_ln_p_demo_55_43_G1_15 d_ln_p_demo_55_43_G1_5 d_ln_p_demo_55_43_G1_22 d_ln_p_demo_55_43_G1_17 d_ln_p_demo_55_43_G1_9 d_ln_p_demo_55_43_G1_27 d_ln_p_demo_55_43_G1_1 d_ln_p_demo_55_43_G1_6 d_ln_p_demo_55_43_G1_20 d_ln_p_demo_55_43_G1_28 d_ln_p_demo_55_43_G1_11 d_ln_p_demo_55_43_G1_21      d_ln_p_demo_55_43_G1_sq_25 d_ln_p_demo_55_43_G1_sq_26 d_ln_p_demo_55_43_G1_sq_3 d_ln_p_demo_55_43_G1_sq_2 d_ln_p_demo_55_43_G1_sq_15 d_ln_p_demo_55_43_G1_sq_5 d_ln_p_demo_55_43_G1_sq_22 d_ln_p_demo_55_43_G1_sq_17 d_ln_p_demo_55_43_G1_sq_9 d_ln_p_demo_55_43_G1_sq_27 d_ln_p_demo_55_43_G1_sq_1 d_ln_p_demo_55_43_G1_sq_6 d_ln_p_demo_55_43_G1_sq_20 d_ln_p_demo_55_43_G1_sq_28 d_ln_p_demo_55_43_G1_sq_11 d_ln_p_demo_55_43_G1_sq_21"

	*pluto:
local d_ln_p_pluto_55_43_G1_squared="d_ln_p_pluto_55_43_G1_25 d_ln_p_pluto_55_43_G1_26 d_ln_p_pluto_55_43_G1_3 d_ln_p_pluto_55_43_G1_2 d_ln_p_pluto_55_43_G1_15 d_ln_p_pluto_55_43_G1_5 d_ln_p_pluto_55_43_G1_22 d_ln_p_pluto_55_43_G1_17 d_ln_p_pluto_55_43_G1_9 d_ln_p_pluto_55_43_G1_27 d_ln_p_pluto_55_43_G1_1 d_ln_p_pluto_55_43_G1_6 d_ln_p_pluto_55_43_G1_20 d_ln_p_pluto_55_43_G1_28 d_ln_p_pluto_55_43_G1_11 d_ln_p_pluto_55_43_G1_21      d_ln_p_pluto_55_43_G1_sq_25 d_ln_p_pluto_55_43_G1_sq_26 d_ln_p_pluto_55_43_G1_sq_3 d_ln_p_pluto_55_43_G1_sq_2 d_ln_p_pluto_55_43_G1_sq_15 d_ln_p_pluto_55_43_G1_sq_5 d_ln_p_pluto_55_43_G1_sq_22 d_ln_p_pluto_55_43_G1_sq_17 d_ln_p_pluto_55_43_G1_sq_9 d_ln_p_pluto_55_43_G1_sq_27 d_ln_p_pluto_55_43_G1_sq_1 d_ln_p_pluto_55_43_G1_sq_6 d_ln_p_pluto_55_43_G1_sq_20 d_ln_p_pluto_55_43_G1_sq_28 d_ln_p_pluto_55_43_G1_sq_11 d_ln_p_pluto_55_43_G1_sq_21 "

	*demo G2:
local d_ln_p_demo_55_43_G2_squared="d_ln_p_demo_55_43_G2_32 d_ln_p_demo_55_43_G2_10 d_ln_p_demo_55_43_G2_33 d_ln_p_demo_55_43_G2_31 d_ln_p_demo_55_43_G2_16 d_ln_p_demo_55_43_G2_34 d_ln_p_demo_55_43_G2_12 d_ln_p_demo_55_43_G2_30 d_ln_p_demo_55_43_G2_18 d_ln_p_demo_55_43_G2_23 d_ln_p_demo_55_43_G2_29 d_ln_p_demo_55_43_G2_24 d_ln_p_demo_55_43_G2_19      d_ln_p_demo_55_43_G2_sq_32 d_ln_p_demo_55_43_G2_sq_10 d_ln_p_demo_55_43_G2_sq_33 d_ln_p_demo_55_43_G2_sq_31 d_ln_p_demo_55_43_G2_sq_16 d_ln_p_demo_55_43_G2_sq_34 d_ln_p_demo_55_43_G2_sq_12 d_ln_p_demo_55_43_G2_sq_30 d_ln_p_demo_55_43_G2_sq_18 d_ln_p_demo_55_43_G2_sq_23 d_ln_p_demo_55_43_G2_sq_29 d_ln_p_demo_55_43_G2_sq_24 d_ln_p_demo_55_43_G2_sq_19 "

	*pluto G2:
local d_ln_p_pluto_55_43_G2_squared="d_ln_p_pluto_55_43_G2_32 d_ln_p_pluto_55_43_G2_10 d_ln_p_pluto_55_43_G2_33 d_ln_p_pluto_55_43_G2_31 d_ln_p_pluto_55_43_G2_16 d_ln_p_pluto_55_43_G2_34 d_ln_p_pluto_55_43_G2_12 d_ln_p_pluto_55_43_G2_30 d_ln_p_pluto_55_43_G2_18 d_ln_p_pluto_55_43_G2_23 d_ln_p_pluto_55_43_G2_29 d_ln_p_pluto_55_43_G2_24 d_ln_p_pluto_55_43_G2_19      d_ln_p_pluto_55_43_G2_sq_32 d_ln_p_pluto_55_43_G2_sq_10 d_ln_p_pluto_55_43_G2_sq_33 d_ln_p_pluto_55_43_G2_sq_31 d_ln_p_pluto_55_43_G2_sq_16 d_ln_p_pluto_55_43_G2_sq_34 d_ln_p_pluto_55_43_G2_sq_12 d_ln_p_pluto_55_43_G2_sq_30 d_ln_p_pluto_55_43_G2_sq_18 d_ln_p_pluto_55_43_G2_sq_23 d_ln_p_pluto_55_43_G2_sq_29 d_ln_p_pluto_55_43_G2_sq_24 d_ln_p_pluto_55_43_G2_sq_19 "

	*demo G3:
local d_ln_p_demo_55_43_G3_squared="d_ln_p_demo_55_43_G3_8 d_ln_p_demo_55_43_G3_4 d_ln_p_demo_55_43_G3_14 d_ln_p_demo_55_43_G3_7 d_ln_p_demo_55_43_G3_13       d_ln_p_demo_55_43_G3_sq_8 d_ln_p_demo_55_43_G3_sq_4 d_ln_p_demo_55_43_G3_sq_14 d_ln_p_demo_55_43_G3_sq_7 d_ln_p_demo_55_43_G3_sq_13    "

	*pluto G3:
local d_ln_p_pluto_55_43_G3_squared="d_ln_p_pluto_55_43_G3_8 d_ln_p_pluto_55_43_G3_4 d_ln_p_pluto_55_43_G3_14 d_ln_p_pluto_55_43_G3_7 d_ln_p_pluto_55_43_G3_13       d_ln_p_pluto_55_43_G3_sq_8 d_ln_p_pluto_55_43_G3_sq_4 d_ln_p_pluto_55_43_G3_sq_14 d_ln_p_pluto_55_43_G3_sq_7 d_ln_p_pluto_55_43_G3_sq_13  "



*Change in relative log prices locals including quadratic and cubic terms:
	*demo:
local d_ln_p_demo_55_43_G1_poly="d_ln_p_demo_55_43_G1_25 d_ln_p_demo_55_43_G1_26 d_ln_p_demo_55_43_G1_3 d_ln_p_demo_55_43_G1_2 d_ln_p_demo_55_43_G1_15 d_ln_p_demo_55_43_G1_5 d_ln_p_demo_55_43_G1_22 d_ln_p_demo_55_43_G1_17 d_ln_p_demo_55_43_G1_9 d_ln_p_demo_55_43_G1_27 d_ln_p_demo_55_43_G1_1 d_ln_p_demo_55_43_G1_6 d_ln_p_demo_55_43_G1_20 d_ln_p_demo_55_43_G1_28 d_ln_p_demo_55_43_G1_11 d_ln_p_demo_55_43_G1_21      d_ln_p_demo_55_43_G1_sq_25 d_ln_p_demo_55_43_G1_sq_26 d_ln_p_demo_55_43_G1_sq_3 d_ln_p_demo_55_43_G1_sq_2 d_ln_p_demo_55_43_G1_sq_15 d_ln_p_demo_55_43_G1_sq_5 d_ln_p_demo_55_43_G1_sq_22 d_ln_p_demo_55_43_G1_sq_17 d_ln_p_demo_55_43_G1_sq_9 d_ln_p_demo_55_43_G1_sq_27 d_ln_p_demo_55_43_G1_sq_1 d_ln_p_demo_55_43_G1_sq_6 d_ln_p_demo_55_43_G1_sq_20 d_ln_p_demo_55_43_G1_sq_28 d_ln_p_demo_55_43_G1_sq_11 d_ln_p_demo_55_43_G1_sq_21       d_ln_p_demo_55_43_G1_cu_25 d_ln_p_demo_55_43_G1_cu_26 d_ln_p_demo_55_43_G1_cu_3 d_ln_p_demo_55_43_G1_cu_2 d_ln_p_demo_55_43_G1_cu_15 d_ln_p_demo_55_43_G1_cu_5 d_ln_p_demo_55_43_G1_cu_22 d_ln_p_demo_55_43_G1_cu_17 d_ln_p_demo_55_43_G1_cu_9 d_ln_p_demo_55_43_G1_cu_27 d_ln_p_demo_55_43_G1_cu_1 d_ln_p_demo_55_43_G1_cu_6 d_ln_p_demo_55_43_G1_cu_20 d_ln_p_demo_55_43_G1_cu_28 d_ln_p_demo_55_43_G1_cu_11 d_ln_p_demo_55_43_G1_cu_21"

	*pluto:
local d_ln_p_pluto_55_43_G1_poly="d_ln_p_pluto_55_43_G1_25 d_ln_p_pluto_55_43_G1_26 d_ln_p_pluto_55_43_G1_3 d_ln_p_pluto_55_43_G1_2 d_ln_p_pluto_55_43_G1_15 d_ln_p_pluto_55_43_G1_5 d_ln_p_pluto_55_43_G1_22 d_ln_p_pluto_55_43_G1_17 d_ln_p_pluto_55_43_G1_9 d_ln_p_pluto_55_43_G1_27 d_ln_p_pluto_55_43_G1_1 d_ln_p_pluto_55_43_G1_6 d_ln_p_pluto_55_43_G1_20 d_ln_p_pluto_55_43_G1_28 d_ln_p_pluto_55_43_G1_11 d_ln_p_pluto_55_43_G1_21      d_ln_p_pluto_55_43_G1_sq_25 d_ln_p_pluto_55_43_G1_sq_26 d_ln_p_pluto_55_43_G1_sq_3 d_ln_p_pluto_55_43_G1_sq_2 d_ln_p_pluto_55_43_G1_sq_15 d_ln_p_pluto_55_43_G1_sq_5 d_ln_p_pluto_55_43_G1_sq_22 d_ln_p_pluto_55_43_G1_sq_17 d_ln_p_pluto_55_43_G1_sq_9 d_ln_p_pluto_55_43_G1_sq_27 d_ln_p_pluto_55_43_G1_sq_1 d_ln_p_pluto_55_43_G1_sq_6 d_ln_p_pluto_55_43_G1_sq_20 d_ln_p_pluto_55_43_G1_sq_28 d_ln_p_pluto_55_43_G1_sq_11 d_ln_p_pluto_55_43_G1_sq_21       d_ln_p_pluto_55_43_G1_cu_25 d_ln_p_pluto_55_43_G1_cu_26 d_ln_p_pluto_55_43_G1_cu_3 d_ln_p_pluto_55_43_G1_cu_2 d_ln_p_pluto_55_43_G1_cu_15 d_ln_p_pluto_55_43_G1_cu_5 d_ln_p_pluto_55_43_G1_cu_22 d_ln_p_pluto_55_43_G1_cu_17 d_ln_p_pluto_55_43_G1_cu_9 d_ln_p_pluto_55_43_G1_cu_27 d_ln_p_pluto_55_43_G1_cu_1 d_ln_p_pluto_55_43_G1_cu_6 d_ln_p_pluto_55_43_G1_cu_20 d_ln_p_pluto_55_43_G1_cu_28 d_ln_p_pluto_55_43_G1_cu_11 d_ln_p_pluto_55_43_G1_cu_21"

	*demo G2:
local d_ln_p_demo_55_43_G2_poly="d_ln_p_demo_55_43_G2_32 d_ln_p_demo_55_43_G2_10 d_ln_p_demo_55_43_G2_33 d_ln_p_demo_55_43_G2_31 d_ln_p_demo_55_43_G2_16 d_ln_p_demo_55_43_G2_34 d_ln_p_demo_55_43_G2_12 d_ln_p_demo_55_43_G2_30 d_ln_p_demo_55_43_G2_18 d_ln_p_demo_55_43_G2_23 d_ln_p_demo_55_43_G2_29 d_ln_p_demo_55_43_G2_24 d_ln_p_demo_55_43_G2_19      d_ln_p_demo_55_43_G2_sq_32 d_ln_p_demo_55_43_G2_sq_10 d_ln_p_demo_55_43_G2_sq_33 d_ln_p_demo_55_43_G2_sq_31 d_ln_p_demo_55_43_G2_sq_16 d_ln_p_demo_55_43_G2_sq_34 d_ln_p_demo_55_43_G2_sq_12 d_ln_p_demo_55_43_G2_sq_30 d_ln_p_demo_55_43_G2_sq_18 d_ln_p_demo_55_43_G2_sq_23 d_ln_p_demo_55_43_G2_sq_29 d_ln_p_demo_55_43_G2_sq_24 d_ln_p_demo_55_43_G2_sq_19     d_ln_p_demo_55_43_G2_cu_32 d_ln_p_demo_55_43_G2_cu_10 d_ln_p_demo_55_43_G2_cu_33 d_ln_p_demo_55_43_G2_cu_31 d_ln_p_demo_55_43_G2_cu_16 d_ln_p_demo_55_43_G2_cu_34 d_ln_p_demo_55_43_G2_cu_12 d_ln_p_demo_55_43_G2_cu_30 d_ln_p_demo_55_43_G2_cu_18 d_ln_p_demo_55_43_G2_cu_23 d_ln_p_demo_55_43_G2_cu_29 d_ln_p_demo_55_43_G2_cu_24 d_ln_p_demo_55_43_G2_cu_19"

	*pluto G2:
local d_ln_p_pluto_55_43_G2_poly="d_ln_p_pluto_55_43_G2_32 d_ln_p_pluto_55_43_G2_10 d_ln_p_pluto_55_43_G2_33 d_ln_p_pluto_55_43_G2_31 d_ln_p_pluto_55_43_G2_16 d_ln_p_pluto_55_43_G2_34 d_ln_p_pluto_55_43_G2_12 d_ln_p_pluto_55_43_G2_30 d_ln_p_pluto_55_43_G2_18 d_ln_p_pluto_55_43_G2_23 d_ln_p_pluto_55_43_G2_29 d_ln_p_pluto_55_43_G2_24 d_ln_p_pluto_55_43_G2_19      d_ln_p_pluto_55_43_G2_sq_32 d_ln_p_pluto_55_43_G2_sq_10 d_ln_p_pluto_55_43_G2_sq_33 d_ln_p_pluto_55_43_G2_sq_31 d_ln_p_pluto_55_43_G2_sq_16 d_ln_p_pluto_55_43_G2_sq_34 d_ln_p_pluto_55_43_G2_sq_12 d_ln_p_pluto_55_43_G2_sq_30 d_ln_p_pluto_55_43_G2_sq_18 d_ln_p_pluto_55_43_G2_sq_23 d_ln_p_pluto_55_43_G2_sq_29 d_ln_p_pluto_55_43_G2_sq_24 d_ln_p_pluto_55_43_G2_sq_19     d_ln_p_pluto_55_43_G2_cu_32 d_ln_p_pluto_55_43_G2_cu_10 d_ln_p_pluto_55_43_G2_cu_33 d_ln_p_pluto_55_43_G2_cu_31 d_ln_p_pluto_55_43_G2_cu_16 d_ln_p_pluto_55_43_G2_cu_34 d_ln_p_pluto_55_43_G2_cu_12 d_ln_p_pluto_55_43_G2_cu_30 d_ln_p_pluto_55_43_G2_cu_18 d_ln_p_pluto_55_43_G2_cu_23 d_ln_p_pluto_55_43_G2_cu_29 d_ln_p_pluto_55_43_G2_cu_24 d_ln_p_pluto_55_43_G2_cu_19"

	*demo G3:
local d_ln_p_demo_55_43_G3_poly="d_ln_p_demo_55_43_G3_8 d_ln_p_demo_55_43_G3_4 d_ln_p_demo_55_43_G3_14 d_ln_p_demo_55_43_G3_7 d_ln_p_demo_55_43_G3_13       d_ln_p_demo_55_43_G3_sq_8 d_ln_p_demo_55_43_G3_sq_4 d_ln_p_demo_55_43_G3_sq_14 d_ln_p_demo_55_43_G3_sq_7 d_ln_p_demo_55_43_G3_sq_13       d_ln_p_demo_55_43_G3_cu_8 d_ln_p_demo_55_43_G3_cu_4 d_ln_p_demo_55_43_G3_cu_14 d_ln_p_demo_55_43_G3_cu_7 d_ln_p_demo_55_43_G3_cu_13    "


	*pluto G3:
local d_ln_p_pluto_55_43_G3_poly="d_ln_p_pluto_55_43_G3_8 d_ln_p_pluto_55_43_G3_4 d_ln_p_pluto_55_43_G3_14 d_ln_p_pluto_55_43_G3_7 d_ln_p_pluto_55_43_G3_13       d_ln_p_pluto_55_43_G3_sq_8 d_ln_p_pluto_55_43_G3_sq_4 d_ln_p_pluto_55_43_G3_sq_14 d_ln_p_pluto_55_43_G3_sq_7 d_ln_p_pluto_55_43_G3_sq_13       d_ln_p_pluto_55_43_G3_cu_8 d_ln_p_pluto_55_43_G3_cu_4 d_ln_p_pluto_55_43_G3_cu_14 d_ln_p_pluto_55_43_G3_cu_7 d_ln_p_pluto_55_43_G3_cu_13    "

}

display "${dump}/QS_V`foldno'/fake_prices_`r'"
merge 1:1 market_id state43 district43 decile_dist  using ${dump}/QS_V`foldno'/fake_prices_`r'.dta, nogen

local controls="ev cv dlogexp_cap"
#delimit ;
******************;
*Fake regressions:;
******************;
if 1==1{;

*Single equation test:;
cap reg  d_ln_NG_1_2_3 wdlog_p_G1 
d_ln_p_demo_55_43_25_fake 
d_ln_p_demo_55_43_26_fake
d_ln_p_demo_55_43_3_fake
d_ln_p_demo_55_43_2_fake
d_ln_p_demo_55_43_15_fake
d_ln_p_demo_55_43_5_fake
d_ln_p_demo_55_43_22_fake
d_ln_p_demo_55_43_17_fake
d_ln_p_demo_55_43_9_fake
d_ln_p_demo_55_43_27_fake
d_ln_p_demo_55_43_1_fake
d_ln_p_demo_55_43_6_fake
d_ln_p_demo_55_43_20_fake
d_ln_p_demo_55_43_28_fake
d_ln_p_demo_55_43_11_fake 
d_ln_p_demo_55_43_21_fake
`controls'  [weight=r4355total_wt] if decile_dist==50 ;


cap test 
d_ln_p_demo_55_43_25_fake=
d_ln_p_demo_55_43_26_fake=
d_ln_p_demo_55_43_3_fake=
d_ln_p_demo_55_43_2_fake=
d_ln_p_demo_55_43_15_fake=
d_ln_p_demo_55_43_5_fake=
d_ln_p_demo_55_43_22_fake=
d_ln_p_demo_55_43_17_fake=
d_ln_p_demo_55_43_9_fake=
d_ln_p_demo_55_43_27_fake=
d_ln_p_demo_55_43_1_fake=
d_ln_p_demo_55_43_6_fake=
d_ln_p_demo_55_43_20_fake=
d_ln_p_demo_55_43_28_fake=
d_ln_p_demo_55_43_11_fake =
d_ln_p_demo_55_43_21_fake =0;
local type "G1 prices";
post `permutation_results_V`foldno'' ("`type'") (`r') (r(F)) (r(df)) (r(p)) (e(N));

*G2 prices:;
*Single equation test:;
cap reg  d_ln_NG_1_2_3 wdlog_p_G2 
d_ln_p_demo_55_43_32_fake 
d_ln_p_demo_55_43_10_fake
d_ln_p_demo_55_43_33_fake
d_ln_p_demo_55_43_31_fake
d_ln_p_demo_55_43_16_fake
d_ln_p_demo_55_43_34_fake
d_ln_p_demo_55_43_12_fake
d_ln_p_demo_55_43_30_fake
d_ln_p_demo_55_43_18_fake
d_ln_p_demo_55_43_23_fake
d_ln_p_demo_55_43_29_fake
d_ln_p_demo_55_43_24_fake
d_ln_p_demo_55_43_19_fake
`controls'  [weight=r4355total_wt] if decile_dist==50;

cap test 
d_ln_p_demo_55_43_32_fake=
d_ln_p_demo_55_43_10_fake=
d_ln_p_demo_55_43_33_fake=
d_ln_p_demo_55_43_31_fake=
d_ln_p_demo_55_43_16_fake=
d_ln_p_demo_55_43_34_fake=
d_ln_p_demo_55_43_12_fake=
d_ln_p_demo_55_43_30_fake=
d_ln_p_demo_55_43_18_fake=
d_ln_p_demo_55_43_23_fake=
d_ln_p_demo_55_43_29_fake=
d_ln_p_demo_55_43_24_fake=
d_ln_p_demo_55_43_19_fake=0;

local type "G2 prices";
post `permutation_results_V`foldno'' ("`type'") (`r') (r(F)) (r(df)) (r(p)) (e(N));




*G3 prices:;
*Single equation test:;
cap reg  d_ln_NG_1_2_3 wdlog_p_G3 
d_ln_p_demo_55_43_8_fake 
d_ln_p_demo_55_43_4_fake
d_ln_p_demo_55_43_14_fake
d_ln_p_demo_55_43_7_fake
d_ln_p_demo_55_43_13_fake
`controls'  [weight=r4355total_wt] if decile_dist==50;

cap test 
d_ln_p_demo_55_43_8_fake=
d_ln_p_demo_55_43_4_fake=
d_ln_p_demo_55_43_14_fake=
d_ln_p_demo_55_43_7_fake=
d_ln_p_demo_55_43_13_fake=0;

local type "G3 prices";
post `permutation_results_V`foldno'' ("`type'") (`r') (r(F)) (r(df)) (r(p)) (e(N));



*Not G1, G2 or G3 expenditures:;
*Single equation test:;
cap reg  d_ln_NG_1_2_3 w_rshare_dlog_p_demo_G1G2G3
d_ln_p_demo_55_43_25_fake 
d_ln_p_demo_55_43_26_fake
d_ln_p_demo_55_43_3_fake
d_ln_p_demo_55_43_2_fake
d_ln_p_demo_55_43_15_fake
d_ln_p_demo_55_43_5_fake
d_ln_p_demo_55_43_22_fake
d_ln_p_demo_55_43_17_fake
d_ln_p_demo_55_43_9_fake
d_ln_p_demo_55_43_27_fake
d_ln_p_demo_55_43_1_fake
d_ln_p_demo_55_43_6_fake
d_ln_p_demo_55_43_20_fake
d_ln_p_demo_55_43_28_fake
d_ln_p_demo_55_43_11_fake 
d_ln_p_demo_55_43_21_fake 
d_ln_p_demo_55_43_32_fake 
d_ln_p_demo_55_43_10_fake
d_ln_p_demo_55_43_33_fake
d_ln_p_demo_55_43_31_fake
d_ln_p_demo_55_43_16_fake
d_ln_p_demo_55_43_34_fake
d_ln_p_demo_55_43_12_fake
d_ln_p_demo_55_43_30_fake
d_ln_p_demo_55_43_18_fake
d_ln_p_demo_55_43_23_fake
d_ln_p_demo_55_43_29_fake
d_ln_p_demo_55_43_24_fake
d_ln_p_demo_55_43_19_fake
d_ln_p_demo_55_43_8_fake 
d_ln_p_demo_55_43_4_fake
d_ln_p_demo_55_43_14_fake
d_ln_p_demo_55_43_7_fake
d_ln_p_demo_55_43_13_fake
`controls'  [weight=r4355total_wt] if decile_dist==50;

cap test d_ln_p_demo_55_43_25_fake=
d_ln_p_demo_55_43_26_fake=
d_ln_p_demo_55_43_3_fake=
d_ln_p_demo_55_43_2_fake=
d_ln_p_demo_55_43_15_fake=
d_ln_p_demo_55_43_5_fake=
d_ln_p_demo_55_43_22_fake=
d_ln_p_demo_55_43_17_fake=
d_ln_p_demo_55_43_9_fake=
d_ln_p_demo_55_43_27_fake=
d_ln_p_demo_55_43_1_fake=
d_ln_p_demo_55_43_6_fake=
d_ln_p_demo_55_43_20_fake=
d_ln_p_demo_55_43_28_fake=
d_ln_p_demo_55_43_11_fake=
d_ln_p_demo_55_43_32_fake=
d_ln_p_demo_55_43_10_fake=
d_ln_p_demo_55_43_33_fake=
d_ln_p_demo_55_43_31_fake=
d_ln_p_demo_55_43_16_fake=
d_ln_p_demo_55_43_34_fake=
d_ln_p_demo_55_43_12_fake=
d_ln_p_demo_55_43_30_fake=
d_ln_p_demo_55_43_18_fake=
d_ln_p_demo_55_43_23_fake=
d_ln_p_demo_55_43_29_fake=
d_ln_p_demo_55_43_24_fake=
d_ln_p_demo_55_43_8_fake=
d_ln_p_demo_55_43_4_fake=
d_ln_p_demo_55_43_14_fake=
d_ln_p_demo_55_43_7_fake=0;

local type "G1 G2 G3 prices";
post `permutation_results_V`foldno'' ("`type'") (`r') (r(F)) (r(df)) (r(p)) (e(N));

*end of fake regressions code:;
};




*End of runs loop:;
};



*****************;
*Real regressions;
*****************;

*G1 prices:;
#delimit ;
*Single equation test:;
cap reg  d_ln_NG_1_2_3 wdlog_p_G1 
d_ln_p_demo_55_43_G1_25 
d_ln_p_demo_55_43_G1_26
d_ln_p_demo_55_43_G1_3
d_ln_p_demo_55_43_G1_2
d_ln_p_demo_55_43_G1_15
d_ln_p_demo_55_43_G1_5
d_ln_p_demo_55_43_G1_22
d_ln_p_demo_55_43_G1_17
d_ln_p_demo_55_43_G1_9
d_ln_p_demo_55_43_G1_27
d_ln_p_demo_55_43_G1_1
d_ln_p_demo_55_43_G1_6
d_ln_p_demo_55_43_G1_20
d_ln_p_demo_55_43_G1_28
d_ln_p_demo_55_43_G1_11 
d_ln_p_demo_55_43_G1_21 `controls'  [weight=r4355total_wt] if decile_dist==50;

cap test d_ln_p_demo_55_43_G1_25=
d_ln_p_demo_55_43_G1_26=
d_ln_p_demo_55_43_G1_3=
d_ln_p_demo_55_43_G1_2=
d_ln_p_demo_55_43_G1_15=
d_ln_p_demo_55_43_G1_5=
d_ln_p_demo_55_43_G1_22=
d_ln_p_demo_55_43_G1_17=
d_ln_p_demo_55_43_G1_9=
d_ln_p_demo_55_43_G1_27=
d_ln_p_demo_55_43_G1_1=
d_ln_p_demo_55_43_G1_6=
d_ln_p_demo_55_43_G1_20=
d_ln_p_demo_55_43_G1_28=
d_ln_p_demo_55_43_G1_11=
d_ln_p_demo_55_43_G1_21=0;
local type "G1 prices";
post `permutation_results_V`foldno'' ("`type'") (0) (r(F)) (r(df)) (r(p)) (e(N));




*G2 prices:;
*Single equation test:;
cap reg  d_ln_NG_1_2_3  wdlog_p_G2 
d_ln_p_demo_55_43_G2_32 
d_ln_p_demo_55_43_G2_10
d_ln_p_demo_55_43_G2_33
d_ln_p_demo_55_43_G2_31
d_ln_p_demo_55_43_G2_16
d_ln_p_demo_55_43_G2_34
d_ln_p_demo_55_43_G2_12
d_ln_p_demo_55_43_G2_30
d_ln_p_demo_55_43_G2_18
d_ln_p_demo_55_43_G2_23
d_ln_p_demo_55_43_G2_29
d_ln_p_demo_55_43_G2_24
d_ln_p_demo_55_43_G2_19
`controls' if decile_dist==50 [weight=r4355total_wt];

cap test 
d_ln_p_demo_55_43_G2_32=
d_ln_p_demo_55_43_G2_10=
d_ln_p_demo_55_43_G2_33=
d_ln_p_demo_55_43_G2_31=
d_ln_p_demo_55_43_G2_16=
d_ln_p_demo_55_43_G2_34=
d_ln_p_demo_55_43_G2_12=
d_ln_p_demo_55_43_G2_30=
d_ln_p_demo_55_43_G2_18=
d_ln_p_demo_55_43_G2_23=
d_ln_p_demo_55_43_G2_29=
d_ln_p_demo_55_43_G2_24=
d_ln_p_demo_55_43_G2_19=0;

local type "G2 prices";
post `permutation_results_V`foldno'' ("`type'") (0) (r(F)) (r(df)) (r(p)) (e(N));





*Not G3 expenditures:;
*Single equation test:;
cap reg  d_ln_NG_1_2_3  wdlog_p_G3 
d_ln_p_demo_55_43_G3_8 
d_ln_p_demo_55_43_G3_4
d_ln_p_demo_55_43_G3_14
d_ln_p_demo_55_43_G3_7
d_ln_p_demo_55_43_G3_13
`controls' [weight=r4355total_wt] if decile_dist==50 ;


cap test 
d_ln_p_demo_55_43_G3_8=
d_ln_p_demo_55_43_G3_4=
d_ln_p_demo_55_43_G3_14=
d_ln_p_demo_55_43_G3_7=
d_ln_p_demo_55_43_G3_13=0;

local type "G3 prices";
post `permutation_results_V`foldno'' ("`type'") (0) (r(F)) (r(df)) (r(p)) (e(N));



*G1, G2 and G3 prices:;
*Single equation test:;
reg  d_ln_NG_1_2_3 w_rshare_dlog_p_demo_G1G2G3 
d_ln_p_demo_55_43_G1_25 
d_ln_p_demo_55_43_G1_26
d_ln_p_demo_55_43_G1_3
d_ln_p_demo_55_43_G1_2
d_ln_p_demo_55_43_G1_15
d_ln_p_demo_55_43_G1_5
d_ln_p_demo_55_43_G1_22
d_ln_p_demo_55_43_G1_17
d_ln_p_demo_55_43_G1_9
d_ln_p_demo_55_43_G1_27
d_ln_p_demo_55_43_G1_1
d_ln_p_demo_55_43_G1_6
d_ln_p_demo_55_43_G1_20
d_ln_p_demo_55_43_G1_28
d_ln_p_demo_55_43_G1_11 
d_ln_p_demo_55_43_G1_21 
d_ln_p_demo_55_43_G2_32 
d_ln_p_demo_55_43_G2_10
d_ln_p_demo_55_43_G2_33
d_ln_p_demo_55_43_G2_31
d_ln_p_demo_55_43_G2_16
d_ln_p_demo_55_43_G2_34
d_ln_p_demo_55_43_G2_12
d_ln_p_demo_55_43_G2_30
d_ln_p_demo_55_43_G2_18
d_ln_p_demo_55_43_G2_23
d_ln_p_demo_55_43_G2_29
d_ln_p_demo_55_43_G2_24
d_ln_p_demo_55_43_G2_19
d_ln_p_demo_55_43_G3_8 
d_ln_p_demo_55_43_G3_4
d_ln_p_demo_55_43_G3_14
d_ln_p_demo_55_43_G3_7
d_ln_p_demo_55_43_G3_13
`controls'  [weight=r4355total_wt] if decile_dist==50;

test 
d_ln_p_demo_55_43_G1_25=
d_ln_p_demo_55_43_G1_26=
d_ln_p_demo_55_43_G1_3=
d_ln_p_demo_55_43_G1_2=
d_ln_p_demo_55_43_G1_15=
d_ln_p_demo_55_43_G1_5=
d_ln_p_demo_55_43_G1_22=
d_ln_p_demo_55_43_G1_17=
d_ln_p_demo_55_43_G1_9=
d_ln_p_demo_55_43_G1_27=
d_ln_p_demo_55_43_G1_1=
d_ln_p_demo_55_43_G1_6=
d_ln_p_demo_55_43_G1_20=
d_ln_p_demo_55_43_G1_28=
d_ln_p_demo_55_43_G1_11=
d_ln_p_demo_55_43_G1_21=
d_ln_p_demo_55_43_G2_32=
d_ln_p_demo_55_43_G2_10=
d_ln_p_demo_55_43_G2_33=
d_ln_p_demo_55_43_G2_31=
d_ln_p_demo_55_43_G2_16=
d_ln_p_demo_55_43_G2_34=
d_ln_p_demo_55_43_G2_12=
d_ln_p_demo_55_43_G2_30=
d_ln_p_demo_55_43_G2_18=
d_ln_p_demo_55_43_G2_23=
d_ln_p_demo_55_43_G2_29=
d_ln_p_demo_55_43_G2_24=
d_ln_p_demo_55_43_G2_19=
d_ln_p_demo_55_43_G3_8=
d_ln_p_demo_55_43_G3_4=
d_ln_p_demo_55_43_G3_14=
d_ln_p_demo_55_43_G3_7=
d_ln_p_demo_55_43_G3_13=0;

local type "G1 G2 G3 prices";
*Note that run 0 corresponds to the real data run;
post `permutation_results_V`foldno'' ("`type'") (0) (r(F)) (r(df)) (r(p)) (e(N));


*end of controls loop:;

#delimit ;
postclose `permutation_results_V`foldno'';
*End of permutation regressions:;

#delimit cr;
}







use "$output\permutation_resultsV`foldno'.dta", clear

local runs=500


keep if type=="G1 G2 G3 prices"
sort run
local vertical=round(F[_n==1],.01)
display `vertical'
local runs=500
sort p
cap drop p_value
gen p_value=(_n/(`runs'+1))
local low=round(F[25],.01)
display `low'
local high=round(F[475],.01)
display `high'
histogram F, xtitle("Test statistic", size(medlarge)) ytitle("Density") xline(`low' `high', lcolor(gray)) xline(`vertical', lcolor(red) lwidth(thick))  fcolor(eltblue%40 erose%25 none none) lcolor(eltblue erose edkblue maroon)  graphregion(lcolor(white) fcolor(white))
graph save  "$output\QS_Test_Panel_B.gph", replace
graph export "$master_file_path\output\QS_Test_Panel_B.pdf", replace as(pdf)
cap log close
log using "$output\p-value_result", replace
display "P-value of joint permutation test"
sum p_value if run==0
cap log close
*Cleanup:
cap erase "$output/QSV.dta"
cap erase "$output/QSV_wide.dta"
cap erase "$output/QSV_wide_4permutation.dta"
cap erase "$output/expenditures_decile_for_QS.dta"
cap erase "$output/permutation_resultsVPanel_B.dta"




